/**
 *
 */
package anysware.gwt.client.ui;

import anysware.gwt.client.rest.CallbackEvent;
import anysware.gwt.client.rest.CallbackListener;
import anysware.gwt.client.rest.RestRequestBuilder;
import anysware.gwt.client.util.PageUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
public class SourceEditorDialog extends DialogBox {
	public static final SourceEditorImageBundle	IMAGE_BUNDLE	= GWT
	                                                                 .create(SourceEditorImageBundle.class);
	private final PushButton	                addBtn;
	private final PushButton	                closeBtn;
	private final PushButton	                delBtn;
	private final SourceEditor	                sourceEditor;
	private JSONArray	                        sourceList;
	private final ListBox	                    sourceListBox;

	public SourceEditorDialog() {
		super(false, true);
		setCaptionText(AnyswareApplicationFrame.CONSTANTS.sourceEditorCaption());
		final VerticalPanel outer = new VerticalPanel();
		final HorizontalPanel panel = new HorizontalPanel();
		sourceListBox = new ListBox(false);
		sourceListBox.addChangeListener(new ChangeListener() {
			public void onChange(final Widget sender) {
				sourceSelected(sourceListBox.getSelectedIndex());
			}
		});
		addBtn = new PushButton(IMAGE_BUNDLE.addIcon().createImage());
		delBtn = new PushButton(IMAGE_BUNDLE.deleteIcon().createImage());
		closeBtn = new PushButton(IMAGE_BUNDLE.closeIcon().createImage());
		closeBtn.addClickListener(new ClickListener() {

			public void onClick(final Widget sender) {
				hide();
			}
		});
		addBtn.addClickListener(new ClickListener() {

			public void onClick(final Widget sender) {
				sourceEditor.addNew();
			}
		});
		delBtn.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {

			}
		});
		panel.add(sourceListBox);
		panel.add(addBtn);
		panel.add(delBtn);
		panel.add(closeBtn);
		outer.add(panel);
		sourceEditor = new SourceEditor(this);
		outer.add(sourceEditor);
		setWidget(outer);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.google.gwt.user.client.ui.Panel#onLoad()
	 */
	@Override
	protected void onLoad() {
		super.onLoad();
		final String endpointURL = PageUtils.getProperty("endpoint.sources");
		final RestRequestBuilder builder = RestRequestBuilder.createBuilder(
		        "GET", endpointURL + ".json");
		builder.setHeader("Accept", "application/json");
		builder.sendRequest(null, new CallbackListener() {
			public void handle(final CallbackEvent event) {
				if (event.getReason().equals(CallbackEvent.Reason.SUCCESS)) {
					final Response response = event.getResponse();
					if (response.getStatusCode() == 200) {
						updateSourceList(JSONParser.parse(response.getText())
						        .isArray());
					}
				}
			}
		});
	}

	protected void sourceSelected(final int selectedIndex) {
		GWT.log("Selected Index=" + selectedIndex, null);
		final JSONObject obj = sourceList.get(selectedIndex - 1).isObject();
		GWT.log("Selected =" + obj, null);
		sourceEditor.setSource(sourceList.get(selectedIndex - 1).isObject());
	}

	protected void updateSourceList(final JSONArray array) {
		GWT.log("SourceList: " + array, null);
		sourceListBox.clear();
		sourceList = array;
		sourceListBox.addItem("", "");
		for (int i = 0; i < sourceList.size(); i++) {
			final String value = sourceList.get(i).isObject().get("name")
			        .isString().stringValue();
			sourceListBox.addItem(value, value);
		}

	}

	void addItem(final JSONObject item) {
		GWT.log("Add new Item: " + item, null);
		sourceList.set(sourceList.size(), item);
		final String name = item.get("name").isString().stringValue();
		sourceListBox.addItem(name, name);
	}
}
